import request from '@/utils/request'

// tags
export const createTag = (tagname) => {
  return request.post('/community/post/tags', {
    name: tagname
  })
}

export const searchTags = (msg, pagenum = 1, pagesize = 20) => {
  return request.get('/community/post/tags', {
    params: {
      message: msg,
      pagenum: pagenum,
      pagesize: pagesize
    }
  })
}

export const searchHotTags = (pagenum = 1, pagesize = 5) => {
  return request.get('/community/post/tags/hot', {
    params: {
      pagenum: pagenum,
      pagesize: pagesize
    }
  })
}

// post
export const addPost = (title, content, tagIds, imgIds) => {
  return request.post('/community/post/add', {
    title: title,
    content: content,
    tagsIds: tagIds,
    imgIds: imgIds
  })
}

export const searchPost = (msg = null, pagenum = 1, pagesize = 20) => {
  return request.get('/community/post/search', {
    params: {
      message: msg,
      pagenum: pagenum,
      pagesize: pagesize
    }
  })
}

export const likePost = (postid) => {
  return request.put('/community/post/like/' + postid)
}

export const favoritePost = (postid) => {
  return request.put('/community/post/favorite/' + postid)
}

// file
export const uploadPostImg = (file) => {
  const formData = new FormData()
  formData.append("file", file)
  return request.post('/community/post/image', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 30000 // 设置 30 秒
  })
}

export const deletePostImg = (imgid) => {
  return request.delete('/community/post/image/' + imgid)
}

// comment
export const commentPost = (postid, content, parentid = 0, replyuser = null) => {
  return request.post('/community/post/comment', {
    postId: postid,
    parentId: parentid,
    content: content,
    replyUser: replyuser
  })
}

export const deletePost = (postid) => {
  return request.delete('community/post/' + postid)
}

export const searchComment = (postid) => {
  return request.get('/community/post/comment/' + postid)
}

export const deletePostComment = (commentid) => {
  return request.delete('/community/post/comment/' + commentid)
}

export const likeComment = (commentid) => {
  return request.put('/community/post/comment/like/' + commentid)
}

export const searchMyPost = (type, pagenum = 1, pagesize = 20) => {
  return request.get('/community/post/me', {
    params: {
      type: type,
      pagenum: pagenum,
      pagesize: pagesize
    }
  })
}

export const searchUserPost = (userid,pagenum,pagesize) => {
  return request.get('/community/post/user',{
    params: {
      userid,
      pagesize,
      pagenum
    }
  })
}